package com.hk.service.order.impl;

import java.util.List;

import com.hk.service.IUserChannel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.Constant;
import com.hk.commons.ConstantRoute;
import com.hk.commons.DateUtil;
import com.hk.commons.HqlFilter;
import com.hk.commons.StringHelper;
import com.hk.commons.enm.ConfirmStatusEnum;
import com.hk.commons.enm.PayStatusEnum;
import com.hk.commons.entity.Bank;
import com.hk.commons.entity.FundInfo_Yimi;
import com.hk.commons.entity.Organization;
import com.hk.commons.entity.order.OrderRouteInfo;
import com.hk.commons.entity.product.Company;
import com.hk.commons.entity.product.Payment;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.entity.yingmi.YingmiConfirmFile;
import com.hk.commons.entity.yingmi.YingmiOrderFile;
import com.hk.commons.utils.order.OrderCreateMsg;
import com.hk.commons.vo.OrderStrMsg;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IFundInfo_Yimi;
import com.hk.service.IOrganization;
import com.hk.service.order.IOrderHandle;
import com.hk.service.order.IOrderRouteInfo;
import com.hk.service.order.IOrderStatements;
import com.hk.service.order.IPayment;
import com.hk.service.product.ICompany;
import com.hk.service.sys.IBank;
import com.hk.service.sys.ISysUser;
import com.hk.service.yingmi.IYingmiConfirmFile;

@Service
public class YingmiOrderHandleImpl extends BaseServiceImpl<YingmiOrderFile>
		implements IOrderHandle<YingmiOrderFile> {

	@Autowired
	private ISysUser userService;

	@Autowired
	private ICompany comService;

	@Autowired
	private IBank bankService;

	@Autowired
	private IUserChannel userChannelService;

	@Autowired
	private IOrganization orgService;

	@Autowired
	private IPayment paymentService;

	@Autowired
	private IFundInfo_Yimi fundInfoService;

	@Autowired
	private IYingmiConfirmFile ymConfirmService;

	@Autowired
	private IOrderStatements orderStatmentService;

	@Autowired
	private IOrderRouteInfo orderRouteService;

	@Override
	public List<YingmiOrderFile> query(HqlFilter hqlFilter) {
		List<YingmiOrderFile> list = super.findByFilter(hqlFilter);
		return list;
	}

	@Override
	public Long createOrderRoute(Long userId, Long orderId, Integer companyId) {

		OrderRouteInfo orderRouteInfo = new OrderRouteInfo();
		orderRouteInfo.setUserId(userId);
		orderRouteInfo.setCompanyId(companyId);
		orderRouteInfo.setOrderId(orderId);
		Long routeInfoId = orderRouteService.add(orderRouteInfo);
		return routeInfoId;
	}

	@Override
	public OrderStrMsg getStrInfoById(Long orderId) {
		// 盈米订单转orderStrMsg
		HqlFilter ymHqlFilter = new HqlFilter();
		ymHqlFilter.addEQFilter("orderId", String.valueOf(orderId));
		ymHqlFilter.addEQFilter("payStatus", 2);// 只展示盈米付款成功的订单。
		List<YingmiConfirmFile> conList = ymConfirmService
				.findByFilter(ymHqlFilter);

		// 如果用户订单数据为空，则返回 null
		if (conList == null || conList.size() == 0) {
			return null;
		}
		YingmiConfirmFile orderInfo = conList.get(0);
		OrderStrMsg orderStrMsg = new OrderStrMsg();

		if (orderInfo != null) {
			// 01加入订单基本属性
			orderStrMsg.setId(orderInfo.getId().toString());
			orderStrMsg.setPayStatus("2");
			orderStrMsg.setPayStatusStr(PayStatusEnum.PAYSTATUS_2.getMsg());// 盈米订单都是确认的。写死。
			orderStrMsg.setConfirmStatus("2");
			orderStrMsg.setConfirmStatusStr(ConfirmStatusEnum.CONFIRMSTATUS_2
					.getMsg());
			if (orderInfo.getSuccAmount() != null) {// 申请金额
				orderStrMsg.setAmount(orderInfo.getSuccAmount()
						.toEngineeringString());
			}

			if (orderInfo.getPayStatus() != null) {
				orderStrMsg.setPayStatus(orderInfo.getPayStatus().toString());
				orderStrMsg.setPayStatusStr(PayStatusEnum
						.getPayStatus(orderInfo.getPayStatus()));
			}

			if (orderInfo.getConfirmStatus() != null) {
				orderStrMsg.setConfirmStatus(orderInfo.getConfirmStatus()
						.toString());
				orderStrMsg.setConfirmStatusStr(ConfirmStatusEnum
						.getConfirmStatus(orderInfo.getConfirmStatus()));
			}

			if (StringHelper.isNotEmpty(orderInfo.getCts())) {

				Long orderCreatOn = DateUtil.transferStringToLong(orderInfo
						.getCts());
				orderStrMsg.setOrderCreatedOn(orderCreatOn.toString());
				orderStrMsg.setOrderCreatedOnStr(DateUtil
						.longToLongDate(orderCreatOn));
			}

			if (StringHelper.isNotEmpty(orderInfo.getConfirmCts())) {
				Long orderConfirmOn = DateUtil.transferStringToLong(orderInfo
						.getConfirmCts());
				orderStrMsg.setOrderConfirmDate(orderConfirmOn.toString());
				orderStrMsg.setOrderConfirmDateStr(DateUtil
						.longToShortDate(orderConfirmOn));
			}
			if (StringHelper.isNotEmpty(orderInfo.getTransactionCts())) {
				Long orderTranOn = DateUtil.transferStringToLong(orderInfo
						.getTransactionCts());
				orderStrMsg.setOrderTradeDate(orderTranOn.toString());
				orderStrMsg.setOrderTradeDateStr(DateUtil
						.longToShortDate(orderTranOn));
			}
			// 02加入顾客 及所属理财师基本属性

			HqlFilter userFilter = new HqlFilter();
			userFilter.addEQFilter("accountId", orderInfo.getAccountId());
			List<SysUser> userList = userService.findByFilter(userFilter);
			if (userList.size() > 0) {
				SysUser user = userList.get(0);
				if (user != null) {
					orderStrMsg.setUserId(user.getId().toString());
					orderStrMsg.setUserName(user.getRealName());
					orderStrMsg.setUserPhoneNum(user.getName());

					// 添加理财师的信息
					SysUser financialPlanner = null;
					orderStrMsg.setChannelName("无主客户");
					if (userService.getFinUserIdByUserId(user.getId()) != null) {
						Long finUserId = userService.getFinUserIdByUserId(user
								.getId());

						if (finUserId != null) {
							financialPlanner = userService.findById(finUserId);
						}

						if (financialPlanner != null) {
							orderStrMsg.setFinancialPlannerId(financialPlanner
									.getId().toString());
							orderStrMsg
									.setFinancialPlannerName(financialPlanner
											.getRealName());
							orderStrMsg
									.setFinancialPlannerPhoneNum(financialPlanner
											.getName());

							// 描述理财师
							if (financialPlanner.getOrgId() != null) {

								Organization org = orgService
										.findById(financialPlanner.getOrgId());
								if (org != null) {
									orderStrMsg.setFinancialPlannerDepName(org
											.getFlag02());
								}

							}

							if (financialPlanner.getChannel() != null) {
								if (orderInfo.getChannel() != null) {
									Integer channelId = orderInfo.getChannel();
									String channelName = userChannelService.getChannelNameByChannelId(channelId);
									orderStrMsg.setChannelName(channelName);
									orderStrMsg.setChannel(channelId.toString());
								}
							}
						}
					}

				}

			}

			String productCode = orderInfo.getFundCode();
			orderStrMsg.setProductCode(productCode);
			orderStrMsg.setProductTopType(String
					.valueOf(Constant.PRODUCT_TYPE_1));
			HqlFilter fundFilter = new HqlFilter();
			fundFilter.addEQFilter("fundCode", productCode);
			List<FundInfo_Yimi> fundNavList = fundInfoService
					.findByFilter(fundFilter);
			if (fundNavList.size() > 0) {
				FundInfo_Yimi fundNav = fundNavList.get(0);
				String fundName = fundNav.getFundName();
				orderStrMsg.setProductId(fundNav.getId());
				orderStrMsg.setProductName(fundName);
				HqlFilter comFilter = new HqlFilter();
				comFilter.addEQFilter("id", ConstantRoute.COMPANY_YINGMI);
				List<Company> comList = comService.findByFilter(comFilter);
				orderStrMsg.setProductCompanyId(String
						.valueOf(ConstantRoute.COMPANY_YINGMI));
				if (comList.size() > 0) {
					orderStrMsg.setProductCompanyName(comList.get(0).getName());
				}
			}

			// 04 加入支付信息
			
			orderStrMsg.setOrderType("盈米扣款");
			
			String paymentMethodId = orderInfo.getPaymentMethodId();
			HqlFilter paymentFilter = new HqlFilter();
			paymentFilter.addEQFilter("paymentMethodId", paymentMethodId);
			List<Payment> payments = paymentService.findByFilter(paymentFilter);
			if (payments.size() > 0) {
				Payment payment = payments.get(0);
				Integer bankId = payment.getBankId();
				if (bankId != null && bankService.existById(bankId)) {
					Bank bank = bankService.findById(bankId);

					orderStrMsg.setPaymentBankName(bank.getBankName());
					orderStrMsg.setPaymentBankNo(bank.getBankCode());
				}

				orderStrMsg.setPaymentBankCardNo(payment.getPaymentNo());
				orderStrMsg.setPaymentUserName(payment.getAccountName());
			}

			// 给定单添加组织架构，并且增加组织架构描述
			if (orderInfo.getOrgId() != null) {
				orderStrMsg.setOrgId(String.valueOf(orderInfo.getOrgId()));
				Organization org = orgService.findById(orderInfo.getOrgId());
				if (org != null) {
					orderStrMsg.setOrgName(org.getFlag02());
				}
			}

		}
		return orderStrMsg;
	}

	@Override
	public YingmiOrderFile findById(long id) {

		YingmiOrderFile oderStrMsg = new YingmiOrderFile();

		return oderStrMsg;
	}

	@Override
	public OrderCreateMsg createOrder(Long productId, Long userId,
			Long paymentId, Long userCardId) {
		// TODO Auto-generated method stub
		return null;
	}
}
